package com.bristua.mall.order.biz;

import com.bristua.mall.order.dao.TOrderGoodsMapper;
import com.bristua.mall.order.dao.TOrderMapper;
import com.bristua.mall.order.model.TOrder;
import com.bristua.mall.order.model.TOrderGoods;
import com.bristua.mall.order.model.condition.TOrderGoodsExample;
import com.bristua.mall.order.model.vo.OrderDetailGoodsVO;
import com.bristua.mall.order.model.vo.QueryOrderDetailVO;
import com.bristua.mall.user.dao.TUserAdressMapper;
import com.bristua.mall.user.model.TUserAdress;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/4/27.
 */
@Service
public class QueryOrderDetailBiz {
    @Resource
    private TUserAdressMapper tUserAdressMapper;

    @Resource
    private TOrderMapper tOrderMapper;

    @Resource
    private TOrderGoodsMapper tOrderGoodsMapper;

    public QueryOrderDetailVO queryOrderDetail(String orderId) {
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(orderId);
        if (tOrder != null) {
            QueryOrderDetailVO queryOrderDetailVO = new QueryOrderDetailVO();
            String userAddressId = tOrder.getUserAddressId();
            if (!StringUtils.isEmpty(userAddressId)) {
                TUserAdress tUserAdress = tUserAdressMapper.selectByPrimaryKey(userAddressId);
                queryOrderDetailVO.getReceiveGoods().setReceiveAddressName(tUserAdress.getReceiveAddressName());
                queryOrderDetailVO.getReceiveGoods().setReceiveGoodsName(tUserAdress.getReceiveGoodsName());
                queryOrderDetailVO.getReceiveGoods().setReceivePhone(tUserAdress.getReceivePhone());
                queryOrderDetailVO.getReceiveGoods().setUserAddressId(tUserAdress.getUserAddressId());
            }
            TOrderGoodsExample tOrderGoodsExample = new TOrderGoodsExample();
            tOrderGoodsExample.createCriteria().andOrderIdEqualTo(tOrder.getOrderId());
            List<TOrderGoods> tOrderGoodss = tOrderGoodsMapper.selectByExample(tOrderGoodsExample);
            if (tOrderGoodss != null && tOrderGoodss.size() > 0) {
                for (TOrderGoods tOrderGoods : tOrderGoodss) {
                    OrderDetailGoodsVO orderDetailGoodsVO = new OrderDetailGoodsVO();
                    orderDetailGoodsVO.setBuyNum(tOrderGoods.getBuyNum());
                    orderDetailGoodsVO.setGoodsImgUrl(tOrderGoods.getGoodsImgUrl());
                    orderDetailGoodsVO.setGoodsPrice(tOrderGoods.getGoodsPrice());
                    orderDetailGoodsVO.setGoodsId(tOrderGoods.getGoodsId());
                    BigDecimal multiply = new BigDecimal(tOrderGoods.getBuyNum()).multiply(new BigDecimal(tOrderGoods.getGoodsPrice()));
                    orderDetailGoodsVO.setAllGoodsPrice(String.valueOf(multiply.longValue()));
                    orderDetailGoodsVO.setProductId(tOrderGoods.getProductId());
                    orderDetailGoodsVO.setGoodsName(tOrderGoods.getGoodsName());
                    orderDetailGoodsVO.setSpecMsg(tOrderGoods.getSpecMsg());
                    queryOrderDetailVO.getGoods().add(orderDetailGoodsVO);
                }
            }

            if("1".equals(tOrder.getPayStaus()) && "1".equals(tOrder.getOrderStatus())){
                queryOrderDetailVO.setStatus(1);
            }

            if("2".equals(tOrder.getPayStaus()) && "1".equals(tOrder.getSendStatus())){
                queryOrderDetailVO.setStatus(2);
            }

            if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "1".equals(tOrder.getConfirmStatus()) ){
                queryOrderDetailVO.setStatus(3);
            }

            if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "2".equals(tOrder.getConfirmStatus())&&"0".equals(tOrder.getEvaluateStatus())){
                queryOrderDetailVO.setStatus(4);
            }

            if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "2".equals(tOrder.getConfirmStatus())&&"1".equals(tOrder.getEvaluateStatus())){
                queryOrderDetailVO.setStatus(5);
            }

            if("1".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getOrderStatus())){
                queryOrderDetailVO.setStatus(6);
            }
            queryOrderDetailVO.setLogisticsNumber(tOrder.getDeliverySn());
            queryOrderDetailVO.setOrderId(tOrder.getOrderId());
            queryOrderDetailVO.setCreateTime(tOrder.getCreateTime());
            queryOrderDetailVO.setPayType(tOrder.getPayType()==null?0:tOrder.getPayType());
            queryOrderDetailVO.setPayFee(StringUtils.isEmpty(tOrder.getPayFee())?"0":tOrder.getPayFee());
            queryOrderDetailVO.setOrderPrice(tOrder.getOrderPrice());
            queryOrderDetailVO.setFreight(tOrder.getFreight());
            return queryOrderDetailVO;
        }
        return null;
    }
}